package com.example.springsercurityservice.controller;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class HelloController implements InitializingBean {

    @Autowired
    private HttpSession httpSession;

    @RequestMapping("/hello")
    public Object hello(){

        //打印用户信息
        SecurityContext context = SecurityContextHolder.getContext();

        Authentication authentication = context.getAuthentication();

        //System.out.println(authentication.toString());

        System.out.println("session:"+httpSession.hashCode()+"==="+httpSession.getAttribute("SPRING_SECURITY_CONTEXT"));
        return "hello";
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //System.out.println("session:"+httpSession.hashCode()+"==="+httpSession.getAttribute("SPRING_SECURITY_CONTEXT"));
    }
}
